package com.wfm.filter;

import com.google.common.base.Charsets;
import com.google.common.hash.Funnels;

/**
 * 布隆过滤器
 */
public class GuavaBloomFilter implements BloomFilter {

    // 100w 个 url，误判率 0.0001
    private static com.google.common.hash.BloomFilter<String> bloomFilter
            = com.google.common.hash.BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 100000000, 0.000001);

    @Override
    public boolean filter(String url) {
        boolean contains = bloomFilter.mightContain(url);
        if (!contains) {
            bloomFilter.put(url);
        }
        return contains;
    }
}
